import { useStore } from '@/store'
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Home from '../views/Home.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/posts/new',
    name: 'postsNew',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('../views/NewPost.vue'),
    meta:{
      requiresAuth:true
    }
  },
  {
    path: '/posts/:id',
    name: 'ShowPost',
    component: () => import('../views/ShowPost.vue')
  },
  {
    path: '/posts/:id/edit',
    name: 'EditPost',
    component: () => import('../views/EditPost.vue'),
    meta:{
      requiresAuth:true
    }
  }
]



const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to,from,next)=>{
  const store=useStore()
  if(to.meta.requiresAuth&&!store.getState().loginUser.currentUserId){
    return next({name:"Home"})
  }
  next()
})

export default router
